/*
 * Copyright Notice ====================================================
 * This file contains proprietary information of JeeStrap Co.
 * Copying or reproduction without prior written approval is prohibited.
 * Copyright (c) 2014 All rights reserved. =============================
 */

package com.jeestrap.common.smartcoder.gui.setting;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import com.jeestrap.common.smartcoder.database.DbUtil;
import com.jeestrap.common.smartcoder.database.DriverInfoLoader;
import com.jeestrap.common.smartcoder.domain.DriverInfo;
import com.jeestrap.common.smartcoder.util.Constants;

public class ProjectSettingPanel extends JPanel {

	/**
	 * serialVersionUID long
	 */
	private static final long serialVersionUID = 290995946262307472L;

	private static String separator = File.separator;

	JTextField baseFolderTextField = new JTextField();

	JFileChooser folderChooser = new JFileChooser();

	JTextField projNameTextField = new JTextField();
	
	String projType = Constants.PROJECT_TYPE_MAVEN;
	
	JCheckBox initProjectCheckBox = new JCheckBox();

	JTextField srcFolderTextField = new JTextField();
	
	JTextField webFolderTextField = new JTextField();

	JTextField basePackageTextField = new JTextField();

	JTextField moduleTextField = new JTextField();

	JTextField subModuleTextField = new JTextField();

	JComboBox driverComboBox = new JComboBox();

	JTextField URLTextField = new JTextField();

	JTextField userTextField = new JTextField();

	JPasswordField passwordTextField = new JPasswordField();

	JTextField driverClassTextField = new JTextField();

	JButton testButton = new JButton();

	JCheckBox entityCheckBox = new JCheckBox();

	JCheckBox daoCheckBox = new JCheckBox();

	JCheckBox serviceCheckBox = new JCheckBox();

	JCheckBox controllerCheckBox = new JCheckBox();

	JCheckBox sqlCheckBox = new JCheckBox();

	JCheckBox queryJspCheckBox = new JCheckBox();

	JCheckBox editspCheckBox = new JCheckBox();

	public ProjectSettingPanel() {
		super();
		setLayout(new GridBagLayout());

		JPanel folderHeadPanel = initProjectSettingPanel();
		this.add(folderHeadPanel, new GridBagConstraints(0, 0, 1, 1, 0.01D, 0.01D, 17, 1, new Insets(0, 0, 0, 0), 0, 0));

		JPanel dbPanel = initDBSettingPanel();
		this.add(dbPanel, new GridBagConstraints(0, 1, 1, 1, 0.01D, 0.01D, 17, 1, new Insets(0, 0, 0, 0), 0, 0));

		JPanel folderFilePanel = initSaveCodeSettingPanel();
		this.add(folderFilePanel, new GridBagConstraints(0, 2, 1, 1, 0.01D, 0.01D, 17, 1, new Insets(0, 0, 0, 0), 0, 0));
	}

	/**
	 * 
	 * 初始化基础配置panel
	 * 
	 * @return
	 * 
	 * @since
	 */
	private JPanel initProjectSettingPanel() {
		JPanel folderHeadPanel = new JPanel();

		// 获取工程路径
		String projectPath = "";
		try {
			projectPath = getClass().getResource("").getFile();
			while (!new File(projectPath + separator + "src" + separator + "main").exists()) {
				projectPath = (new File(projectPath)).getParentFile().getAbsolutePath();
			}
			System.out.println("Project Path: {}" + separator + "src");
		} catch (Exception e) {
			System.out.println("Project Path Error :" + e.getMessage());
		}

		folderHeadPanel.setLayout(new GridBagLayout());

		TitledBorder titledBorder4 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white, new Color(148, 145,
			140)), "项目基础设置");
		folderHeadPanel.setBorder(titledBorder4);

		JLabel projNameLabel = new JLabel("项目名称:");
		folderHeadPanel.add(projNameLabel, new GridBagConstraints(0, 0, 1, 1, 0.0D, 0.0D, 10, 0,
			new Insets(0, 0, 5, 5), 0, 0));

		projNameTextField.setColumns(20);
		folderHeadPanel.add(projNameTextField, new GridBagConstraints(1, 0, 1, 1, 0.0D, 0.0D, 10, 0, new Insets(0, 0,
			5, 5), 0, 0));

		ButtonGroup buttonGroup = new ButtonGroup();
		JRadioButton rdbtnMaven = new JRadioButton("Maven项目");
		rdbtnMaven.setSelected(true);
		rdbtnMaven.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				srcFolderTextField.setText("src\\main\\java");
				webFolderTextField.setText("src\\main\\webapp\\views");
				
				webFolderTextField.setEditable(false);
				srcFolderTextField.setEditable(false);
				
				projType = Constants.PROJECT_TYPE_MAVEN;
			}
		});
		GridBagConstraints gbc_rdbtnMaven = new GridBagConstraints();
		gbc_rdbtnMaven.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnMaven.gridx = 2;
		gbc_rdbtnMaven.gridy = 0;
		folderHeadPanel.add(rdbtnMaven, gbc_rdbtnMaven);
		buttonGroup.add(rdbtnMaven);

		JRadioButton rdbtnweb = new JRadioButton("普通Web项目");
		rdbtnweb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				srcFolderTextField.setText("src");
				webFolderTextField.setText("webapp\\views");
				
				webFolderTextField.setEditable(true);
				srcFolderTextField.setEditable(true);
				projType = Constants.PROJECT_TYPE_JAVA;
			}
		});

		GridBagConstraints gbc_rdbtnweb = new GridBagConstraints();
		gbc_rdbtnweb.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnweb.gridx = 3;
		gbc_rdbtnweb.gridy = 0;
		gbc_rdbtnMaven.anchor = GridBagConstraints.WEST;
		folderHeadPanel.add(rdbtnweb, gbc_rdbtnweb);
		buttonGroup.add(rdbtnweb);
		
		initProjectCheckBox.setSelected(false);
		initProjectCheckBox.setText("初始化项目?");
		initProjectCheckBox.setBackground(Color.GREEN);
		GridBagConstraints gbc_initProjectCheckBox = new GridBagConstraints();
		gbc_initProjectCheckBox.insets = new Insets(0, 0, 5, 5);
		gbc_initProjectCheckBox.gridx = 4;
		gbc_initProjectCheckBox.gridy = 0;
		gbc_initProjectCheckBox.anchor = GridBagConstraints.WEST;
		folderHeadPanel.add(initProjectCheckBox, gbc_initProjectCheckBox);
		
		JLabel outFolderLabel = new JLabel();
		outFolderLabel.setText("项目目录:");
		folderHeadPanel.add(outFolderLabel, new GridBagConstraints(0, 1, 1, 1, 0.0D, 0.0D, 10, 0,
			new Insets(0, 0, 5, 5), 0, 0));
		
		baseFolderTextField.setColumns(50);
		baseFolderTextField.setToolTipText("项目保存路径");
		GridBagConstraints gbc_baseFolderTextField = new GridBagConstraints();
		gbc_baseFolderTextField.insets = new Insets(0, 0, 5, 5);
		gbc_baseFolderTextField.gridx = 1;
		gbc_baseFolderTextField.gridy = 1;
		gbc_baseFolderTextField.gridwidth=3;
		gbc_baseFolderTextField.anchor = GridBagConstraints.WEST;
		folderHeadPanel.add(baseFolderTextField, gbc_baseFolderTextField);

		JButton folderCooseButton = new JButton();
	    this.folderChooser.setCurrentDirectory(new File("."));
	    this.folderChooser.setFileSelectionMode(1);
		folderCooseButton.setText("...");
		folderCooseButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int i = folderChooser.showOpenDialog(ProjectSettingPanel.this);
				if ((i == 0) && (folderChooser.getSelectedFile() != null)) {
					baseFolderTextField.setText(folderChooser.getSelectedFile().getPath());
				}
			}
		});
		folderHeadPanel.add(folderCooseButton, new GridBagConstraints(4, 1, 1, 1, 0.0D, 0.0D, 10, 0, new Insets(0, 0,
			5, 5), 0, 0));

		JLabel srcLabel = new JLabel("Src目录:");
		folderHeadPanel.add(srcLabel, new GridBagConstraints(0, 2, 1, 1, 0.0D, 0.0D, 10, 0, new Insets(0, 0, 5, 5), 0,
			0));

		srcFolderTextField.setColumns(20);
		folderHeadPanel.add(srcFolderTextField, new GridBagConstraints(1, 2, 1, 1, 0.0D, 0.0D, 10, 0, new Insets(0, 0,
			5, 5), 0, 0));

		JLabel webLabel = new JLabel("Web目录:");
		folderHeadPanel.add(webLabel, new GridBagConstraints(2, 2, 1, 1, 0.0D, 0.0D, 10, 0, new Insets(0, 0, 5, 5), 0,
			0));

		webFolderTextField.setColumns(20);
		folderHeadPanel.add(webFolderTextField, new GridBagConstraints(3, 2, 1, 1, 0.0D, 0.0D, 10,
			GridBagConstraints.WEST, new Insets(0, 0, 5, 5), 0, 0));

		JLabel packageLabel = new JLabel("基础包名:");
		folderHeadPanel.add(packageLabel, new GridBagConstraints(0, 3, 1, 1, 0.0D, 0.0D, 10, 0, new Insets(0, 0, 5, 5),
			0, 0));

		basePackageTextField.setColumns(20);
		folderHeadPanel.add(basePackageTextField, new GridBagConstraints(1, 3, 1, 1, 0.0D, 0.0D, 10, 0, new Insets(0,
			0, 0, 5), 0, 0));

		JLabel moduleLabel = new JLabel("模块名:");
		folderHeadPanel.add(moduleLabel, new GridBagConstraints(2, 3, 1, 1, 0.0D, 0.0D, 10, 0, new Insets(0, 0, 5, 5),
			0, 0));

		moduleTextField.setColumns(20);
		folderHeadPanel.add(moduleTextField, new GridBagConstraints(3, 3, 1, 1, 0.0D, 0.0D, 10, 0, new Insets(0, 0, 5, 5), 0, 0));

		JLabel subModuleLabel = new JLabel("子模块名:");
		folderHeadPanel.add(subModuleLabel, new GridBagConstraints(4, 3, 1, 1, 0.0D, 0.0D, 10, 0,
			new Insets(0, 0, 5, 5), 0, 0));

		subModuleTextField.setColumns(10);
		folderHeadPanel.add(subModuleTextField, new GridBagConstraints(5, 3, 1, 1, 0.0D, 0.0D, 10, 0, new Insets(0, 0, 5, 5), 0, 0));
		return folderHeadPanel;
	}

	/**
	 * 初始化保存代码配置panel
	 * 
	 * @return
	 * 
	 * @since
	 */
	private JPanel initDBSettingPanel() {
		JPanel driverPanel = new JPanel();
		driverPanel.setLayout(new GridBagLayout());

		TitledBorder titledBorder4 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white, new Color(148, 145,
			140)), "数据库配置");
		driverPanel.setBorder(titledBorder4);

		JLabel driverTypeLabel = new JLabel("驱动类型:");
		GridBagConstraints gbc_driverTypeLabel = new GridBagConstraints();
		gbc_driverTypeLabel.insets = new Insets(0, 0, 5, 5);
		gbc_driverTypeLabel.gridx = 0;
		gbc_driverTypeLabel.gridy = 0;
		driverPanel.add(driverTypeLabel, gbc_driverTypeLabel);

		this.driverComboBox.setEditable(true);
		List localList = DriverInfoLoader.loadJDBCInfo();
		for (int i = 0; i < localList.size(); i++) {
			this.driverComboBox.addItem(localList.get(i));
			this.driverComboBox.setSelectedIndex(0);
		}
		driverComboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent paramAnonymousItemEvent) {
				if ((driverComboBox.getSelectedItem() instanceof DriverInfo)) {
					DriverInfo localDriverInfo = (DriverInfo)driverComboBox.getSelectedItem();
					driverClassTextField.setText(localDriverInfo.getDriver());
					URLTextField.setText(localDriverInfo.getUrl());
					userTextField.setText(localDriverInfo.getUser());
					passwordTextField.setText(localDriverInfo.getPassword());
				}
			}
		});

		GridBagConstraints gbc_driverComboBox = new GridBagConstraints();
		gbc_driverComboBox.insets = new Insets(0, 0, 5, 5);
		gbc_driverComboBox.gridx = 1;
		gbc_driverComboBox.gridy = 0;
		gbc_driverComboBox.anchor = GridBagConstraints.WEST;
		driverPanel.add(driverComboBox, gbc_driverComboBox);

		JLabel driverClassLabel = new JLabel("驱动类名:");
		GridBagConstraints gbc_driverClassLabel = new GridBagConstraints();
		gbc_driverClassLabel.insets = new Insets(0, 0, 5, 5);
		gbc_driverClassLabel.gridx = 2;
		gbc_driverClassLabel.gridy = 0;
		driverPanel.add(driverClassLabel, gbc_driverClassLabel);

		driverClassTextField.setColumns(15);
		GridBagConstraints gbc_driverClassTextField = new GridBagConstraints();
		gbc_driverClassTextField.insets = new Insets(0, 0, 5, 5);
		gbc_driverClassTextField.gridx = 3;
		gbc_driverClassTextField.gridy = 0;
		driverPanel.add(driverClassTextField, gbc_driverClassTextField);

		JLabel urlLabel = new JLabel("连接URL:");
		GridBagConstraints gbc_urlLabel = new GridBagConstraints();
		gbc_urlLabel.insets = new Insets(0, 0, 5, 5);
		gbc_urlLabel.gridx = 4;
		gbc_urlLabel.gridy = 0;
		driverPanel.add(urlLabel, gbc_urlLabel);

		URLTextField.setColumns(15);
		GridBagConstraints gbc_URLTextField = new GridBagConstraints();
		gbc_URLTextField.insets = new Insets(0, 0, 5, 5);
		gbc_URLTextField.gridx = 5;
		gbc_URLTextField.gridy = 0;
		driverPanel.add(URLTextField, gbc_URLTextField);

		JLabel userLabel = new JLabel("连接用户:");
		GridBagConstraints gbc_userLabel = new GridBagConstraints();
		gbc_userLabel.insets = new Insets(0, 0, 5, 5);
		gbc_userLabel.gridx = 0;
		gbc_userLabel.gridy = 1;
		driverPanel.add(userLabel, gbc_userLabel);

		userTextField.setColumns(15);
		GridBagConstraints gbc_userTextField = new GridBagConstraints();
		gbc_userTextField.insets = new Insets(0, 0, 5, 5);
		gbc_userTextField.gridx = 1;
		gbc_userTextField.gridy = 1;
		driverPanel.add(userTextField, gbc_userTextField);

		JLabel pwdLabel = new JLabel("连接密码:");
		GridBagConstraints gbc_pwdLabel = new GridBagConstraints();
		gbc_pwdLabel.insets = new Insets(0, 0, 5, 5);
		gbc_pwdLabel.gridx = 2;
		gbc_pwdLabel.gridy = 1;
		driverPanel.add(pwdLabel, gbc_pwdLabel);

		passwordTextField.setColumns(15);
		GridBagConstraints gbc_passwordTextField = new GridBagConstraints();
		gbc_passwordTextField.insets = new Insets(0, 0, 5, 5);
		gbc_passwordTextField.gridx = 3;
		gbc_passwordTextField.gridy = 1;
		driverPanel.add(passwordTextField, gbc_passwordTextField);

		testButton.setText("测试连接");
		testButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DriverInfo localDriverInfo = (DriverInfo)driverComboBox.getSelectedItem();
				String dbName = null;
				if (localDriverInfo != null) {
					dbName = localDriverInfo.getDbName();
				}
				String driver = ProjectSettingPanel.this.driverClassTextField.getText().trim();
				String url = ProjectSettingPanel.this.URLTextField.getText().trim();
				String user = ProjectSettingPanel.this.userTextField.getText().trim();
				String password = new String(ProjectSettingPanel.this.passwordTextField.getPassword());

				try {
					DbUtil.getDBConnection(dbName, driver, url, user, password);
					JOptionPane.showMessageDialog(ProjectSettingPanel.this, "   测试成功  ", "信息", 1);
				} catch (Throwable localThrowable) {
					localThrowable.printStackTrace();
					JOptionPane.showMessageDialog(ProjectSettingPanel.this, localThrowable.getMessage(), "Error", 0);
				}
			}
		});
		GridBagConstraints gbc_testButton = new GridBagConstraints();
		gbc_pwdLabel.insets = new Insets(0, 0, 5, 5);
		gbc_testButton.gridx = 4;
		gbc_testButton.gridy = 1;
		gbc_testButton.gridwidth = 2;
		gbc_testButton.anchor = GridBagConstraints.WEST;
		driverPanel.add(testButton, gbc_testButton);

		return driverPanel;
	}

	/**
	 * 初始化保存代码配置panel
	 * 
	 * @return
	 * 
	 * @since
	 */
	private JPanel initSaveCodeSettingPanel() {
		JPanel folderFilePanel = new JPanel();
		folderFilePanel.setLayout(new GridBagLayout());

		TitledBorder titledBorder4 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white, new Color(148, 145,
			140)), "保存代码设置");
		folderFilePanel.setBorder(titledBorder4);

		entityCheckBox.setSelected(true);
		entityCheckBox.setText("Entity");

		controllerCheckBox.setSelected(true);
		controllerCheckBox.setText("Controller");

		queryJspCheckBox.setSelected(true);
		queryJspCheckBox.setText("查询页面");

		editspCheckBox.setSelected(true);
		editspCheckBox.setText("编辑页面");

		serviceCheckBox.setSelected(false);
		serviceCheckBox.setText("Service");

		daoCheckBox.setSelected(false);
		daoCheckBox.setText("Dao");

		sqlCheckBox.setSelected(false);
		sqlCheckBox.setText("建表Sql");

		folderFilePanel.add(entityCheckBox, new GridBagConstraints(0, 0, 1, 1, 0.0D, 0.0D, 17, 0,
			new Insets(0, 0, 0, 0), 0, 0));
		folderFilePanel.add(controllerCheckBox, new GridBagConstraints(1, 0, 1, 1, 0.0D, 0.0D, 17, 0, new Insets(0, 0,
			0, 0), 0, 0));

		folderFilePanel.add(queryJspCheckBox, new GridBagConstraints(2, 0, 1, 1, 0.0D, 0.0D, 17, 0, new Insets(0, 0, 0,
			0), 0, 0));

		folderFilePanel.add(editspCheckBox, new GridBagConstraints(3, 0, 1, 1, 0.0D, 0.0D, 17, 0,
			new Insets(0, 0, 0, 0), 0, 0));

		folderFilePanel.add(serviceCheckBox, new GridBagConstraints(4, 0, 1, 1, 0.0D, 0.0D, 17, 0, new Insets(0, 0, 0,
			0), 0, 0));
		folderFilePanel.add(daoCheckBox, new GridBagConstraints(5, 0, 1, 1, 0.0D, 0.0D, 17, 0, new Insets(0, 0, 0, 0),
			0, 0));
		folderFilePanel.add(sqlCheckBox, new GridBagConstraints(6, 0, 1, 1, 0.0D, 0.0D, 17, 0, new Insets(0, 0, 0, 0),
			0, 0));
		return folderFilePanel;
	}
}
